using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MyClever.Lib;

namespace MyClever.DefaultPlugins
{
    public class URLBuilder : MissionPlugin
    {
        [Package(Direction.Input, "Defines the urls, which should setup with some queries.")]
        enum Deeplinks 
        {
            [Column(typeof(string), "The URL of the website, where do you like add your queries.",
                Validation = "isURL")]
            URL
        }
        
        [Package(Direction.Input, "Defines the queries which you like to add to the url.")]
        enum Queries { }

        [Package(Direction.Output, "Contains all generated URLs.")]
        enum Websites
        {
            [Column(typeof(string), "The generated website addresses.",
                Validation = "isURL")]
            URL
        }

        [Plugin(PluginDevStatus.Development, "This plugin generates new URLs by adding your favorite deeplinks with some queries.")]
        public URLBuilder()
        {
        }


        public override void Initialize()
        {
            //This is the first Function called by the host...
            //Put anything needed to start with here first
        }

        public override void Dispose()
        {
            //Put any cleanup code in here for when the program is stopped
        }

        protected override void Work()
        {
            DataTableReader deeplinks = this.GetDataReader(typeof(Deeplinks));
            while (deeplinks.Read())
            {
                DataTableReader queries = this.GetDataReader(typeof(Queries));
                while (queries.Read())
                {
                    StringBuilder resultURL = new StringBuilder(1000);
                    resultURL.Append(deeplinks[Deeplinks.URL.ToString()].ToString());
                    
                    // start the queries with the correct operator
                    char startQueryOperator = '?';
                    if (deeplinks[Deeplinks.URL.ToString()].ToString().IndexOf('?') >= 0)
                    {
                        startQueryOperator = '&';
                    }
                    resultURL.Append(startQueryOperator);

                    this.AddNewLoggingMessage(typeof(Deeplinks), String.Format("add queries to: '{0}'", resultURL.ToString()));
                    
                    List<KeyValuePair<string, Type>> columns = this.GetColumnNames(queries);
                    foreach (KeyValuePair<string, Type> columnOn in columns)
                    {                        
                        resultURL.AppendFormat("{0}={1}&", columnOn.Key, queries[columnOn.Key].ToString());
                    }

                    //delete last &
                    resultURL = resultURL.Remove(resultURL.Length - 1, 1);

                    DataRow newRow = this.CreateNewRowFromPackage(typeof(Websites));
                    newRow[Websites.URL.ToString()] = Uri.EscapeUriString(resultURL.ToString());
                    this.AddNewRowToPackage(typeof(Websites), newRow);
                }
            }
        }
    }
}
